﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.IO.Ports;
using System.Diagnostics;


namespace call2sms
{
    /// <summary>
    /// Interaction logic for WinConfig.xaml
    /// </summary>
    public partial class WinConfig : Window
    {
        string[] ports;

        public Config config = null;

        public Config theConfig
        {
            get{return config;}
            set { config = value; }
        }

        public WinConfig()
        {
            InitializeComponent();
        }

        private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {
            if(comboBox2 != null) comboBox2.IsEnabled = true;
            if(textBox1 != null) textBox1.IsEnabled = true;
        }

        private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
        {
            comboBox2.IsEnabled = false;
            textBox1.IsEnabled = false;

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            config.MonitorCom = comboBox1.SelectedValue as string;
            config.Notify = (bool)checkBox1.IsChecked ;
            config.SendCom = comboBox2.SelectedValue as string;
            config.Receiver = textBox1.Text;
            this.DialogResult = true;
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            enum_port();

        }

        private void enum_port()
        {
            ports = SerialPort.GetPortNames();
            comboBox1.ItemsSource = ports;
            comboBox2.ItemsSource = ports;
            comboBox1.SelectedValue = config.MonitorCom;
            comboBox2.SelectedValue = config.SendCom;
            checkBox1.IsChecked = config.Notify;
            textBox1.Text = config.Receiver;
        }

        private bool TestPort(string portname)
        {
            bool res = true;
            string str;
            GSMPort port = new GSMPort(portname);
            try
            {
                port.Init();
                if (port.IsOpen())
                {
                    str = port.ExecCommand("AT", 300, "error exec at");
                    if (str.IndexOf("OK") == -1)
                    {
                        textBlock1.Text = "端口不支持指令?";
                        res = false;
                    }
                    port.Close();
                }
                else
                {
                    textBlock1.Text = "端口无法打开.";
                    res = false;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex);
                textBlock1.Text = "端口" + portname + "无响应.";
                 res = false;
            }
            finally
            {
                port.Close();
            }
            return res;

        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            bool res = TestPort(comboBox1.SelectedValue as string);
            if(res)
                res = res && TestPort(comboBox2.SelectedValue as string);
            if(res)
                textBlock1.Text = "好，没问题.";
        }
    }
}
